import java.awt.Dimension;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;

import javax.swing.DefaultListModel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;


public class MainScreen {
	DefaultListModel lm;
	static String personSelected;
	static String personId;
	static String theUser;
	JList personList;

	public JFrame frmMainScreen;
	private JTextField search;
	JButton btnEdit;
	JButton btnAdmin;
	
	ArrayList<String> ids;
	private JButton btnAddPerson;
	private JButton del;
	private JButton btnLogOut;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Login window = new Login();
					window.frmLogIn.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 * @wbp.parser.entryPoint
	 */
	public MainScreen() {
		initialize();
		Database db = new Database();
		ArrayList<Person> peeps = db.getAllPersons();
		populateList(peeps);
		theUser = Login.daUser;
		checkPermissions();
	}
	
	public void checkPermissions(){
		Database db = new Database();
		String perms = db.permissions(theUser);
		if(!perms.contains("w")){
			btnEdit.setVisible(false);
			btnAddPerson.setVisible(false);
		}
		if(!perms.contains("a")){
			btnAdmin.setVisible(false);
			del.setVisible(false);
		}
	}
	
	public void populateList(ArrayList<Person> p){
		lm.clear();
		ids = new ArrayList<String>();
		for(int i = 0; i < p.size(); i++){
			String name = p.get(i).fname + " " + p.get(i).lname;
			ids.add(p.get(i).id);
			lm.addElement(name);
		}
	}
	
	public void searching(){
		ArrayList<Person> list;
		String str = search.getText();
		Database db = new Database();
		if(str.contains(" ")){
			String delims = "[ ]+";
			String[] names = str.split(delims);
			String first = "";
			String last = names[names.length-1];
			if(names.length > 2){
				for(int i = 0; i < names.length; i++){
					if(i != names.length - 1){
						if(i != names.length - 2){
							first = first + names[i] + " ";
						}else{
							first = first + names[i];
						}
					}
				}
			}else{
				first = names[0];
			}
			list = db.lookupName(first,last);
		}else{
			list = db.lookupName(str);
		}
		populateList(list);
	}
	
	public void deletePerson(){
		Database db = new Database();
		boolean result = db.deletePerson(ids.get(personList.getSelectedIndex()));
		System.out.println(result);
		if(result){
			String desc = "Deleted " + personList.getSelectedValue().toString();
			result = db.updateEdits(theUser, desc);
			ArrayList<Person> p = db.getAllPersons();
			searching();
		}
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frmMainScreen = new JFrame();
		frmMainScreen.setTitle("Main Screen");
		frmMainScreen.setBounds(100, 100, 435, 478);
		frmMainScreen.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frmMainScreen.getContentPane().setLayout(null);
		
		lm = new DefaultListModel();
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(22, 32, 182, 391);
		frmMainScreen.getContentPane().add(scrollPane);
		
		personList = new JList(lm);
		scrollPane.setViewportView(personList);
		
		search = new JTextField();
		search.setBounds(237, 31, 154, 20);
		frmMainScreen.getContentPane().add(search);
		search.setColumns(10);
		
		JButton btnSearch = new JButton("Search");
		btnSearch.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				searching();
			}
		});
		btnSearch.setBounds(302, 60, 89, 23);
		frmMainScreen.getContentPane().add(btnSearch);
		
		JButton btnInfo = new JButton("Info");
		btnInfo.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if(personList.getSelectedValue() != null){
					personSelected = personList.getSelectedValue().toString();
					personId = ids.get(personList.getSelectedIndex());
					OptionWindow window = new OptionWindow();
					window.frame.setVisible(true);
				}
			}
		});
		btnInfo.setBounds(320, 372, 89, 23);
		frmMainScreen.getContentPane().add(btnInfo);
		
		btnEdit = new JButton("Edit");
		btnEdit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(personList.getSelectedValue() != null){
					personSelected = personList.getSelectedValue().toString();
					personId = ids.get(personList.getSelectedIndex());
					Edit ed = new Edit();
					ed.frame.setVisible(true);
				}
			}
		});
		btnEdit.setBounds(320, 344, 89, 23);
		frmMainScreen.getContentPane().add(btnEdit);
		
		btnAdmin = new JButton("Admin");
		btnAdmin.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Admin a = new Admin();
				a.frmAdmin.setVisible(true);
			}
		});
		btnAdmin.setBounds(320, 316, 89, 23);
		frmMainScreen.getContentPane().add(btnAdmin);
		
		btnAddPerson = new JButton("Add Person");
		btnAddPerson.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Add a = new Add();
				a.frmAddPerson.setVisible(true);
			}
		});
		btnAddPerson.setBounds(214, 400, 102, 23);
		frmMainScreen.getContentPane().add(btnAddPerson);
		
		del = new JButton("Delete");
		del.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(personList.getSelectedValue() != null){
					deletePerson();
				}
			}
		});
		del.setBounds(214, 372, 102, 23);
		frmMainScreen.getContentPane().add(del);
		
		btnLogOut = new JButton("Log Out");
		btnLogOut.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				frmMainScreen.setVisible(false);
				Login window = new Login();
				window.frmLogIn.setVisible(true);
			}
		});
		btnLogOut.setBounds(320, 400, 89, 23);
		frmMainScreen.getContentPane().add(btnLogOut);
		
	}
}
